// SPDX-License-Identifier: MPL-2.0
// (c) Hare authors <https://harelang.org>

use bytes;
use crypto::mac;

// example taken from the Poly1305-AES paper from D. J. Bernstein
@test fn example1() void = {
	const message: [_]u8 = [0xf3, 0xf6];

	const key: [32]u8 = [
		0x85, 0x1f, 0xc4, 0x0c, 0x34, 0x67, 0xac, 0x0b,
		0xe0, 0x5c, 0xc2, 0x04, 0x04, 0xf3, 0xf7, 0x00,
		0x58, 0x0b, 0x3b, 0x0f, 0x94, 0x47, 0xbb, 0x1e,
		0x69, 0xd0, 0x95, 0xb5, 0x92, 0x8b, 0x6d, 0xbc,
	];

	const expected: [_]u8 = [
		0xf4, 0xc6, 0x33, 0xc3, 0x04, 0x4f, 0xc1, 0x45,
		0xf8, 0x4f, 0x33, 0x5c, 0xb8, 0x19, 0x53, 0xde,
	];

	let result: [16]u8 = [0...];

	let p = poly1305();
	init(&p, &key);
	mac::write(&p, message);
	mac::sum(&p, result);
	mac::finish(&p);

	assert(bytes::equal(expected, result));
};

// example taken from the Poly1305-AES paper from D. J. Bernstein
@test fn example2() void = {
	const key: [_]u8 = [
		0xa0, 0xf3, 0x08, 0x00, 0x00, 0xf4, 0x64, 0x00,
		0xd0, 0xc7, 0xe9, 0x07, 0x6c, 0x83, 0x44, 0x03,
		0xdd, 0x3f, 0xab, 0x22, 0x51, 0xf1, 0x1a, 0xc7,
		0x59, 0xf0, 0x88, 0x71, 0x29, 0xcc, 0x2e, 0xe7,
	];

	const expected: [_]u8 = [
		0xdd, 0x3f, 0xab, 0x22, 0x51, 0xf1, 0x1a, 0xc7,
		0x59, 0xf0, 0x88, 0x71, 0x29, 0xcc, 0x2e, 0xe7,
	];

	let result: [16]u8 = [0...];

	let p = poly1305();
	init(&p, &key);
	mac::sum(&p, result);
	mac::finish(&p);

	assert(bytes::equal(expected, result));
};

// example taken from the Poly1305-AES paper from D. J. Bernstein
@test fn example3() void = {
	const message: [_]u8 = [
		0x66, 0x3c, 0xea, 0x19, 0x0f, 0xfb, 0x83, 0xd8,
		0x95, 0x93, 0xf3, 0xf4, 0x76, 0xb6, 0xbc, 0x24,
		0xd7, 0xe6, 0x79, 0x10, 0x7e, 0xa2, 0x6a, 0xdb,
		0x8c, 0xaf, 0x66, 0x52, 0xd0, 0x65, 0x61, 0x36,
	];

	const expected: [_]u8 = [
		0x0e, 0xe1, 0xc1, 0x6b, 0xb7, 0x3f, 0x0f, 0x4f,
		0xd1, 0x98, 0x81, 0x75, 0x3c, 0x01, 0xcd, 0xbe,
	];

	const key: [32]u8 = [
		0x48, 0x44, 0x3d, 0x0b, 0xb0, 0xd2, 0x11, 0x09,
		0xc8, 0x9a, 0x10, 0x0b, 0x5c, 0xe2, 0xc2, 0x08,
		0x83, 0x14, 0x9c, 0x69, 0xb5, 0x61, 0xdd, 0x88,
		0x29, 0x8a, 0x17, 0x98, 0xb1, 0x07, 0x16, 0xef,
	];

	let result: [16]u8 = [0...];

	let p = poly1305();
	init(&p, &key);
	mac::write(&p, message);
	mac::sum(&p, result);
	mac::finish(&p);

	assert(bytes::equal(expected, result));
};

// example taken from the Poly1305-AES paper from D. J. Bernstein
@test fn example4() void = {
	const message: [_]u8 = [
		0xab, 0x08, 0x12, 0x72, 0x4a, 0x7f, 0x1e, 0x34, 0x27, 0x42,
		0xcb, 0xed, 0x37, 0x4d, 0x94, 0xd1, 0x36, 0xc6, 0xb8, 0x79,
		0x5d, 0x45, 0xb3, 0x81, 0x98, 0x30, 0xf2, 0xc0, 0x44, 0x91,
		0xfa, 0xf0, 0x99, 0x0c, 0x62, 0xe4, 0x8b, 0x80, 0x18, 0xb2,
		0xc3, 0xe4, 0xa0, 0xfa, 0x31, 0x34, 0xcb, 0x67, 0xfa, 0x83,
		0xe1, 0x58, 0xc9, 0x94, 0xd9, 0x61, 0xc4, 0xcb, 0x21, 0x09,
		0x5c, 0x1b, 0xf9,
	];

	const expected: [_]u8 = [
		0x51, 0x54, 0xad, 0x0d, 0x2c, 0xb2, 0x6e, 0x01,
		0x27, 0x4f, 0xc5, 0x11, 0x48, 0x49, 0x1f, 0x1b,
	];

	const key: [32]u8 = [
		0x12, 0x97, 0x6a, 0x08, 0xc4, 0x42, 0x6d, 0x0c,
		0xe8, 0xa8, 0x24, 0x07, 0xc4, 0xf4, 0x82, 0x07,
		0x80, 0xf8, 0xc2, 0x0a, 0xa7, 0x12, 0x02, 0xd1,
		0xe2, 0x91, 0x79, 0xcb, 0xcb, 0x55, 0x5a, 0x57,
	];

	let result: [16]u8 = [0...];

	let p = poly1305();
	init(&p, &key);
	mac::write(&p, message);
	mac::sum(&p, result);
	mac::finish(&p);

	assert(bytes::equal(expected, result));
};

@test fn writepatterns() void = {
	const message: [_]u8 = [
		0xab, 0x08, 0x12, 0x72, 0x4a, 0x7f, 0x1e, 0x34, 0x27, 0x42,
		0xcb, 0xed, 0x37, 0x4d, 0x94, 0xd1, 0x36, 0xc6, 0xb8, 0x79,
		0x5d, 0x45, 0xb3, 0x81, 0x98, 0x30, 0xf2, 0xc0, 0x44, 0x91,
		0xfa, 0xf0, 0x99, 0x0c, 0x62, 0xe4, 0x8b, 0x80, 0x18, 0xb2,
		0xc3, 0xe4, 0xa0, 0xfa, 0x31, 0x34, 0xcb, 0x67, 0xfa, 0x83,
		0xe1, 0x58, 0xc9, 0x94, 0xd9, 0x61, 0xc4, 0xcb, 0x21, 0x09,
		0x5c, 0x1b, 0xf9,
	];

	const expected: [_]u8 = [
		0x51, 0x54, 0xad, 0x0d, 0x2c, 0xb2, 0x6e, 0x01,
		0x27, 0x4f, 0xc5, 0x11, 0x48, 0x49, 0x1f, 0x1b,
	];

	const key: [32]u8 = [
		0x12, 0x97, 0x6a, 0x08, 0xc4, 0x42, 0x6d, 0x0c,
		0xe8, 0xa8, 0x24, 0x07, 0xc4, 0xf4, 0x82, 0x07,
		0x80, 0xf8, 0xc2, 0x0a, 0xa7, 0x12, 0x02, 0xd1,
		0xe2, 0x91, 0x79, 0xcb, 0xcb, 0x55, 0x5a, 0x57,
	];


	patternwrite(&key, message[..], expected[..], [5, 20, 38]);
	patternwrite(&key, message[..], expected[..], [1, 2, 8, 10]);
	patternwrite(&key, message[..], expected[..], [16, 16]);
	patternwrite(&key, message[..], expected[..], [12, 4, 14, 2, 8, 8]);
};

fn patternwrite(key: *key, msg: []u8, expected: []u8, pattern: []uint) void = {
	let p = poly1305();
	init(&p, key);

	for (let i = 0z; i < len(pattern); i += 1) {
		let n = pattern[i];
		mac::write(&p, msg[..n]);
		msg = msg[n..];
	};

	if (len(msg) > 0) {
		mac::write(&p, msg);
	};

	let result: [16]u8 = [0...];
	mac::sum(&p, result);
	mac::finish(&p);

	assert(bytes::equal(expected, result));
};
